package com.Bitco.into.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;

import java.util.ArrayList;

import com.Bitco.into.R;
import com.Bitco.into.entity.TabEntity;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.event.SwitchEvent;
import tech.com.commoncore.utils.ToastUtil;

@Route(path = "/app/main")
public class MainActivity extends BaseTitleActivity {

    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;

    private long oneOutTime = 0;

    private ArrayList<Fragment> fragments;

    private CommonTabLayout mMainTab;

    private int oldSelectPosition = -1;
    private int mCurrentIndex;
    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setBgDrawable(null)
                .setLeftTextDrawable(null);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_main;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initView(Bundle savedInstanceState) {
        mMainTab = findViewById(R.id.main_tab);

        mMainTab.setTabData(getTabEntities());
        mMainTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectFragment(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mFragmentManager = getSupportFragmentManager();

        buildAllFragment();
        selectFragment(0);

//        ARouter.getInstance().build(USER_PHONE_PWD_LOGIN).greenChannel().navigation();

    }

    private void buildAllFragment() {
        fragments = new ArrayList<>();
        addFragment(ServiceFactory.getInstance().getHomeService().newEntryFragment(null));
        addFragment(ServiceFactory.getInstance().getBiService().getHqFragment(null));
//        addFragment(ServiceFactory.getInstance().getCircleService().getCircleListFragment(null));
        addFragment(ServiceFactory.getInstance().getZixunService().newZiXunFragment(null));
        addFragment(ServiceFactory.getInstance().getPerconalService().newEntryFragment(null));
        addFragment(null);
    }

    private void addFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        mTransaction = mFragmentManager.beginTransaction();
        mTransaction.add(R.id.fl_main_content, fragment);
        mTransaction.commit();
        fragments.add(fragment);
    }

    private void selectFragment(int position) {
        mTransaction = mFragmentManager.beginTransaction();
        hideUnSelectFragment(mTransaction);
        if (position > fragments.size()) {
            return;
        }
        Fragment selectFragment = fragments.get(position);
        if (selectFragment != null) {
            mTransaction.show(selectFragment);
        }
        mTransaction.commit();
        oldSelectPosition = position;
    }

    private void hideUnSelectFragment(FragmentTransaction mTransaction) {
        for (Fragment unSelectFragment : fragments) {
            if (unSelectFragment != null) {
                mTransaction.hide(unSelectFragment);
            }
        }
    }

    private ArrayList<CustomTabEntity> getTabEntities() {
        ArrayList<CustomTabEntity> tab = new ArrayList<>();
        tab.add(new TabEntity(getResources().getString(R.string.tab_home), R.mipmap.tab_icon_sy2, R.mipmap.tab_icon_sy));
        tab.add(new TabEntity(getResources().getString(R.string.tab_market), R.mipmap.tab_icon_cl2, R.mipmap.tab_icon_cl));
        tab.add(new TabEntity(getResources().getString(R.string.tab_zixun), R.mipmap.tab_icon_gy2, R.mipmap.tab_icon_gy));
        tab.add(new TabEntity(getResources().getString(R.string.tab_my), R.mipmap.tab_icon_wd2, R.mipmap.tab_icon_wd));
        return tab;
    }

    @Override
    public boolean isRegisterEvent() {
        return true;
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        long time = System.currentTimeMillis();
        if ((time - oneOutTime > 1000)) {
            ToastUtil.show(getResources().getString(R.string.tab_tuichu));
            oneOutTime = time;
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
    @Subscribe
    public void switchTab(IEvent event) {
        if (event instanceof SwitchEvent) {
            mCurrentIndex = ((SwitchEvent) event).position;
            selectFragment(mCurrentIndex);
            mMainTab.setCurrentTab(mCurrentIndex);
        }
    }

}
